package adminorg;

import java.io.Serializable;

import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Container.Filter;

public class EqualToUserFilter implements Filter, Serializable {

	private static final long serialVersionUID = 4173952495190676910L;
	private String columnName;
	private DatabaseHelper dbHelp;
    public EqualToUserFilter(String columnName,DatabaseHelper dbHelp)
    {
    	this.dbHelp = dbHelp;
    	this.columnName = columnName;
    }
	@Override
	public boolean passesFilter(Object itemId, Item item)
			throws UnsupportedOperationException {
		final Property p = item.getItemProperty(columnName);
        if (null == p) {
            return false;
        }
        Object value = p.getValue();
        if (value==null) return false;
        return value.equals(dbHelp.currentUser); 
	}

	@Override
	public boolean appliesToProperty(Object propertyId) {
		return columnName.equals(propertyId);
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((columnName == null) ? 0 : columnName.hashCode());
		result = prime * result + ((dbHelp == null) ? 0 : dbHelp.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EqualToUserFilter other = (EqualToUserFilter) obj;
		if (columnName == null) {
			if (other.columnName != null)
				return false;
		} else if (!columnName.equals(other.columnName))
			return false;
		if (dbHelp == null) {
			if (other.dbHelp != null)
				return false;
		} else if (!dbHelp.equals(other.dbHelp))
			return false;
		return true;
	}

}
